<?php
if(!defined('BASEPATH'))
	exit('No direct script access allowed');

class Torneo extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/torneo/
	 *	- or -
	 * 		http://example.com/index.php/torneo/crear
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function create() {

		$sql = "SELECT category_id, category_desc, category_sport FROM category";

		$options = array(0 => "");

		$query = $this -> db -> query($sql);

		foreach($query->result() as $row) {
			$options[$row -> category_id] = $row -> category_desc . " - " . $row -> category_sport;
		}

		$data['category'] = 0;

		$data['options'] = $options;

		$data['js'] = 'id="category_id"';
		#$this -> load -> view('form_view', $data);
		//$data['category'] = array('name' => 'category', 'id' => 'category');
		$data['sport'] = array('name' => 'sport', 'id' => 'sport');
		$this -> load -> view('torneo_create_view', $data);
	}

	public function submit() {

		/* TODO need to check form validation */

		/*
		 tournament_id
		 category_id
		 tournament_name
		 tournament_init_date
		 tournament_end_date

		 */

		 $time_init = strtotime( $_POST['torneo_init']);
		 $time_end = strtotime( $_POST['torneo_end']);
		
		//$timestamp = date('Y-m-d', $time_init);
		 
		$dataQ = array('tournament_name' => $_POST['torneo_name'], 
						'category_id' => $_POST['category_id'], 
						'tournament_init_date' => date('Y-m-d', $time_init), 
						'tournament_end_date' => date('Y-m-d', $time_end),
						'active_flag' => TRUE,
						'duration' => $_POST['torneo_duration']
						);
						
		$this -> db -> insert('tournament', $dataQ);
		$torneo_id = $this -> db -> insert_id();

		/*
		 tournament_id
		 team_id
		 modifier_user
		 */

		$teamsrel = array();

		foreach($_POST['teams'] as $value) {
			array_push($teamsrel, array('tournament_id' => $torneo_id, 'team_id' => $value));
		}

		$this -> db -> insert_batch('tournament_teams_rel', $teamsrel);

		$teamsrel = array('');
		$data['url'] = "torneo/create";
		$this -> load -> view('success', $data, FALSE);
	}

	public function lista() {

		$this -> load -> model('Torneo_Model');

		$results = $this -> Torneo_Model -> get_tournaments_active();

		foreach($results as $row) {
			echo "<table>";
			echo "<tr>";
			print_r($row);
			echo "</tr>";
			echo "</table>";
		}

	}

	public function equipos($tournamentId=FALSE) {

		$this -> load -> model('Torneo_Model');

		$results = $this -> Torneo_Model -> get_teams_on_tournament($tournamentId);

		foreach($results as $row) {
			echo "<table>";
			echo "<tr>";
			print_r($row);
			echo "</tr>";
			echo "</table>";
		}

	}

}

/* End of file torneo.php */
/* Location: ./application/controllers/torneo.php */
